#include "CEgg.h"
#include "Constants.h"

CEgg* CEgg::CreateEggBYFileName(const char* filename) {
	CEgg* sprite = new CEgg();

	if (sprite && sprite->initWithFile(filename)) {
		sprite->autorelease();
		sprite->setStartSpeed();
		sprite->setBindable(true);
		return sprite;
	}

	CC_SAFE_DELETE(sprite);

	return NULL;
}

Nests* CEgg::getBindingNest() {
	return _nests;
}

void CEgg::setBindable(bool bdable) {
	bindable = bdable;
}

bool CEgg::getBindable() {
	return bindable;
}

void CEgg::bindtoNests(Nests* nests) {
	_nests = nests;
	_state = eggStateFlying;
	_speedY = startSpeed;
	_speedX = 0;
	this->setZOrder(48);
	this->setRotation(0);
}

float CEgg::getYSpeed() {
	return _speedY;
}
bool CEgg::isNestMovingOrNot() {
	return _isNetstMoving;
}
void CEgg::setNetstMoving(bool isNetstMoving) {
	_isNetstMoving = isNetstMoving;
}
void CEgg::updateRobin(float dt) {
	if (_state == eggStateMoving) {
		float distanceY = 0;
		float newspeedY = 0;

		float distanceX = 0;

		distanceY = _speedY * dt + 0.5 * GRAVITY * dt * dt;
		newspeedY = _speedY + GRAVITY * dt;

		distanceX = _speedX * dt;

		if (!isNestMovingOrNot())
			this->setPositionY(this->getPositionY() + distanceY);
		_speedY = newspeedY;

		this ->setPositionX(this->getPositionX() + distanceX);

		this->setRotation(this->getRotation() + dt * 360);

	} else if (_state == eggStateFlying) {
		this->setPositionX(_nests->getPositionX());
		this->setPositionY(_nests->getPositionY()
				+ _nests->getContentSize().height / 2);
	}
}

void CEgg::reset() {
	_state = eggStateFlying;
	this->setPositionX(_nests->getPositionX());
	this->setPositionY(_nests->getPositionY() + _nests->getContentSize().height
			/ 2);
	setStartSpeed();
}

int CEgg::getState() {
	return _state;
}

void CEgg::setSpeedX(float speed) {
	_speedX = speed;
}

void CEgg::setState(int state) {
	_state = state;

}

void CEgg::setStartSpeed() {
	_speedX = 0;
	_speedY = startSpeed;
}

